package q513_findBottomLeftValue;

import CommonClass.Common.TreeNode;

import java.util.Deque;
import java.util.LinkedList;

public class Solution_1 {
    public int findBottomLeftValue(TreeNode root) {
        // 至少有一个节点 因此不需要判空
        Deque<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int ans = 0;
        while (!queue.isEmpty()) {
            int levelSize = queue.size();

            for (int i = 0; i < levelSize; i++) {
                TreeNode node = queue.poll();
                if (i == 0) {
                    ans = node.val;
                }

                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }
        return ans;
    }
}
